package com.ruoyi.message.service.impl;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.bean.BeanUtils;
import com.ruoyi.message.domain.TMessageUser;
import com.ruoyi.message.domain.vo.MessageVo;
import com.ruoyi.message.mapper.TMessageUserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.message.mapper.TMessageMapper;
import com.ruoyi.message.domain.TMessage;
import com.ruoyi.message.service.ITMessageService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 消息推送Service业务层处理
 *
 * @author Bandit
 * @date 2022-11-01
 */
@Service
public class TMessageServiceImpl implements ITMessageService
{
    @Autowired
    private TMessageMapper tMessageMapper;

    @Autowired
    private TMessageUserMapper tMessageUserMapper;

    /**
     * 查询消息推送
     *
     * @param id 消息推送主键
     * @return 消息推送
     */
    @Override
    public TMessage selectTMessageById(Long id)
    {
        return tMessageMapper.selectTMessageById(id);
    }

    /**
     * 查询消息推送列表
     *
     * @param tMessage 消息推送
     * @return 消息推送
     */
    @Override
    public List<TMessage> selectTMessageList(TMessage tMessage)
    {
        return tMessageMapper.selectTMessageList(tMessage);
    }

    /**
     * 新增消息推送
     *
     * @param tMessage 消息推送
     * @return 结果
     */
    @Override
    public int insertTMessage(TMessage tMessage)
    {
        return tMessageMapper.insertTMessage(tMessage);
    }

    @Override
    @Transactional
    public int insertMessageVo(MessageVo messageVo) {
        // 获取消息接收者列表
        List<Long> receiverIds = messageVo.getReceiverIds();
        TMessage message = new TMessage();
        // 拷贝元素
        BeanUtils.copyBeanProp(message, messageVo);
        // 发送者信息
        message.setSenderId(SecurityUtils.getUserId());
        message.setSenderName(SecurityUtils.getUsername());
        message.setReceiveUserCount((long) receiverIds.size());
        message.setReadCount(0L);
        tMessageMapper.insertTMessage(message);
        // 插入后获取messageId
        Long messageId = message.getId();
        List<TMessageUser> tMessageUserList = receiverIds.stream().map(uid -> {
            TMessageUser messageUser = new TMessageUser();
            messageUser.setMessageId(messageId);
            messageUser.setReceiveUserId(uid);
            messageUser.setReaded(0);
            messageUser.setCreateTime(new Date());
            return messageUser;
        }).collect(Collectors.toList());
        return tMessageUserMapper.insertTMessageUserBatch(tMessageUserList);
    }

    /**
     * 修改消息推送
     *
     * @param tMessage 消息推送
     * @return 结果
     */
    @Override
    public int updateTMessage(TMessage tMessage)
    {
        return tMessageMapper.updateTMessage(tMessage);
    }

    /**
     * 批量删除消息推送
     *
     * @param ids 需要删除的消息推送主键
     * @return 结果
     */
    @Override
    public int deleteTMessageByIds(Long[] ids)
    {
        return tMessageMapper.deleteTMessageByIds(ids);
    }

    /**
     * 删除消息推送信息
     *
     * @param id 消息推送主键
     * @return 结果
     */
    @Override
    public int deleteTMessageById(Long id)
    {
        return tMessageMapper.deleteTMessageById(id);
    }

    @Override
    @Transactional
    public int readMessage(Long userId, Long messageId) {
        tMessageMapper.updateReadCount(messageId);
        return tMessageUserMapper.updateRead(userId, messageId);
    }

}
